//package cn.liujianbo.springsecurity.config;
//
//import io.jsonwebtoken.Claims;
//import io.jsonwebtoken.Jwts;
//import io.jsonwebtoken.SignatureAlgorithm;
//import io.jsonwebtoken.io.Decoders;
//import io.jsonwebtoken.security.Keys;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.PostConstruct;
//import java.nio.charset.StandardCharsets;
//import java.security.Key;
//import java.util.Collection;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author 刘建波
// * @date 2022-04-30 22:25
// */
//@Component
//@Slf4j
//public class JWTProvider {
//    private Key key;	// 私钥
//    private long tokenValidityInMilliseconds; // 有效时间
//    private long tokenValidityInMillisecondsForRememberMe; // 记住我有效时间
//    @Autowired
//    private JJWTProperties jjwtProperties; // jwt配置参数
//    @Autowired
//    private UserRepository userRepository;
//    @PostConstruct
//    public void init() {
//        byte[] keyBytes;
//        String secret = jjwtProperties.getSecret();
//        if (StringUtils.hasText(secret)) {
//            log.warn("Warning: the JWT key used is not Base64-encoded. " +
//                    "We recommend using the `jhipster.security.authentication.jwt.base64-secret` key for optimum security.");
//            keyBytes = secret.getBytes(StandardCharsets.UTF_8);
//        } else {
//            log.debug("Using a Base64-encoded JWT secret key");
//            keyBytes = Decoders.BASE64.decode(jjwtProperties.getBase64Secret());
//        }
//        this.key = Keys.hmacShaKeyFor(keyBytes); // 使用mac-sha算法的密钥
//        this.tokenValidityInMilliseconds =
//                1000 * jjwtProperties.getTokenValidityInSeconds();
//        this.tokenValidityInMillisecondsForRememberMe =
//                1000 * jjwtProperties.getTokenValidityInSecondsForRememberMe();
//    }
//    public String createToken(Authentication authentication, boolean rememberMe) {
//        long now = (new Date()).getTime();
//        Date validity;
//        if (rememberMe) {
//            validity = new Date(now + this.tokenValidityInMillisecondsForRememberMe);
//        } else {
//            validity = new Date(now + this.tokenValidityInMilliseconds);
//        }
//        User user = userRepository.findOneByLogin(authentication.getName());
//        Map<String ,Object> map = new HashMap<>();
//        map.put("sub",authentication.getName());
//        map.put("user",user);
//        return Jwts.builder()
//                .setClaims(map) // 添加body
//                .signWith(key, SignatureAlgorithm.HS512) // 指定摘要算法
//                .setExpiration(validity) // 设置有效时间
//                .compact();
//    }
//    public Authentication getAuthentication(String token) {
//        Claims claims = Jwts.parserBuilder()
//                .setSigningKey(key)
//                .build()
//                .parseClaimsJws(token).getBody(); // 根据token获取body
//        User principal;
//        Collection<? extends GrantedAuthority> authorities;
//        principal = userRepository.findOneByLogin(claims.getSubject());
//        authorities = principal.getAuthorities();
//        return new UsernamePasswordAuthenticationToken(principal, token, authorities);
//    }
//}
//
